package com.lcw.library.imagepicker;

import android.app.Activity;
import android.content.Intent;

import androidx.fragment.app.Fragment;

import com.lcw.library.imagepicker.activity.ImagePickerActivity;
import com.lcw.library.imagepicker.manager.ConfigManager;
import com.lcw.library.imagepicker.manager.PermissionManager;
import com.lcw.library.imagepicker.utils.ImageLoader;

import java.util.ArrayList;

/**
 * 图片选择器统一调用入口
 * <p>
 * 提供链式调用API，支持配置各种选项：
 * - 标题设置
 * - 相机开关
 * - 图片/视频显示开关
 * - 最大选择数量
 * - 图片加载器
 * - 历史选择记录
 * - 权限说明弹窗开关
 * <p>
 * 使用示例：
 * ImagePicker.getInstance()
 * .setTitle("选择图片")
 * .showCamera(true)
 * .showPermissionDialog(false)
 * .setMaxCount(9)
 * .setImageLoader(new GlideImageLoader())
 * .start(this, REQUEST_CODE);
 *
 */
public class ImagePicker {

    public static final String EXTRA_SELECT_IMAGES = "selectItems";

    private static volatile ImagePicker mImagePicker;

    private ImagePicker() {
    }

    /**
     * 创建对象
     *
     * @return
     */
    public static ImagePicker getInstance() {
        if (mImagePicker == null) {
            synchronized (ImagePicker.class) {
                if (mImagePicker == null) {
                    mImagePicker = new ImagePicker();
                }
            }
        }
        return mImagePicker;
    }


    /**
     * 设置标题
     *
     * @param title
     * @return
     */
    public ImagePicker setTitle(String title) {
        ConfigManager.getInstance().setTitle(title);
        return mImagePicker;
    }

    /**
     * 是否支持相机
     *
     * @param showCamera
     * @return
     */
    public ImagePicker showCamera(boolean showCamera) {
        ConfigManager.getInstance().setShowCamera(showCamera);
        return mImagePicker;
    }

    /**
     * 是否展示图片
     *
     * @param showImage
     * @return
     */
    public ImagePicker showImage(boolean showImage) {
        ConfigManager.getInstance().setShowImage(showImage);
        return mImagePicker;
    }

    /**
     * 是否展示视频
     *
     * @param showVideo
     * @return
     */
    public ImagePicker showVideo(boolean showVideo) {
        ConfigManager.getInstance().setShowVideo(showVideo);
        return mImagePicker;
    }

    /**
     * 是否过滤GIF图片(默认不过滤)
     *
     * @param filterGif
     * @return
     */
    public ImagePicker filterGif(boolean filterGif) {
        ConfigManager.getInstance().setFilterGif(filterGif);
        return mImagePicker;
    }


    /**
     * 图片最大选择数
     *
     * @param maxCount
     * @return
     */
    public ImagePicker setMaxCount(int maxCount) {
        ConfigManager.getInstance().setMaxCount(maxCount);
        return mImagePicker;
    }

    /**
     * 视频最大选择数
     *
     * @param maxVideoCount
     * @return
     */
    public ImagePicker setMaxVideoCount(int maxVideoCount) {
        ConfigManager.getInstance().setMaxVideoCount(maxVideoCount);
        return mImagePicker;
    }

    /**
     * 设置单类型选择（只能选图片或者视频）
     *
     * @param isSingleType
     * @return
     */
    public ImagePicker setSingleType(boolean isSingleType) {
        ConfigManager.getInstance().setSingleType(isSingleType);
        return mImagePicker;
    }

    /**
     * 设置是否显示权限说明弹窗
     *
     * @param showPermissionDialog true显示，false不显示
     * @return
     */
    public ImagePicker showPermissionDialog(boolean showPermissionDialog) {
        ConfigManager.getInstance().setShowPermissionDialog(showPermissionDialog);
        return mImagePicker;
    }


    /**
     * 设置图片加载器
     *
     * @param imageLoader
     * @return
     */
    public ImagePicker setImageLoader(ImageLoader imageLoader) {
        ConfigManager.getInstance().setImageLoader(imageLoader);
        return mImagePicker;
    }

    /**
     * 设置图片选择历史记录
     *
     * @param imagePaths
     * @return
     */
    public ImagePicker setImagePaths(ArrayList<String> imagePaths) {
        ConfigManager.getInstance().setImagePaths(imagePaths);
        return mImagePicker;
    }

    /**
     * 启动图片选择器 (Activity)
     *
     * @param activity    Activity实例
     * @param requestCode 请求码
     */
    public void start(Activity activity, int requestCode) {
        startImagePicker(activity, requestCode);
    }

    /**
     * 启动图片选择器 (Fragment)
     *
     * @param fragment    Fragment实例
     * @param requestCode 请求码
     */
    public void start(Fragment fragment, int requestCode) {
        if (fragment.getActivity() != null) {
            startImagePicker(fragment.getActivity(), requestCode);
        }
    }

    /**
     * 启动图片选择器Activity
     *
     * @param activity    Activity实例
     * @param requestCode 请求码
     */
    private void startImagePicker(Activity activity, int requestCode) {
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(activity, ImagePickerActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

}
